<%@page import="java.util.Iterator"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="java.util.Date"%>
<%@page import="cn.bdqn.pojo.News"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="../../common/common.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<% 
	//接收、处理请求 
	//设置请求的编码格式 
	request.setCharacterEncoding("utf-8"); 
	 
	News news = new News();
	news.setCreateDate(new Date()); 
	//在服务器端判断该请求是否是文件上传类型 
	boolean flag = ServletFileUpload.isMultipartContent(request); 
	//获取项目在服务器上部署的真实路径（从根目录开始）
	String uploadPath = request.getSession().getServletContext().getRealPath("upload/"); 
	System.out.println(uploadPath); 
	 
	File file = new File(uploadPath); 
	if(!file.exists()){ 
		//若不存在自动创建 
		file.mkdir(); 
	} 
	 
	if(flag){//解析请求并处理 
		System.out.print("flag =============== ");
		//创建文件上传的核心组件 
		//实例化一个硬盘文件工厂，用来配置文件上传组件
		FileItemFactory factory  = new DiskFileItemFactory();
		//通过factory对象来构建文件上传组件ServletFileUpload
		ServletFileUpload upload = new ServletFileUpload(factory);
		
		//解析请求
		List<FileItem> items = upload.parseRequest(request);
		//获取迭代器
	 	Iterator<FileItem> ites= items.iterator();
	 	
	 	while(ites.hasNext()){
	 		FileItem item = ites.next();
	 		//判断FileItem是文件域还是文本域
	 		if(item.isFormField()){//若是文本表单域
	 			String name = item.getFieldName();//获取文本表单域的name的值
	 			if(name.equals("categoryId")){
	 				String categoryId = item.getString();  //获取文本表单域value的值
	 				news.setCategoryId(Integer.parseInt(categoryId));
	 			}else if(name.equals("title")){
	 				String title = item.getString("UTF-8");//获取文本表单域value的值
	 				news.setTitle(title);
	 			}else if(name.equals("author")){
	 				String author = item.getString("UTF-8");//获取文本表单域value的值
	 				news.setAuthor(author);
	 			}else if(name.equals("summary")){
	 				String summary = item.getString("UTF-8");//获取文本表单域value的值
	 				news.setSummary(summary);
	 			}else if(name.equals("newscontent")){
	 				String content = item.getString("UTF-8");//获取文本表单域value的值
	 				news.setContent(content);
	 			}
	 		}else{//文件表单域
	 			String fileName = item.getName();//上传的文件名称（文件路径+文件名）
	 			if(fileName != null && !fileName.equals("")){//文件上传
	 				//userId+系统时间+随机数.doc
	 				File fullFile = new File(fileName);
	 				//把文件上传到指定目录下
	 				File saveFile = new File(uploadPath,fullFile.getName());
	 				System.out.println("fileName=== > " + fullFile.getName());
	 				item.write(saveFile);//上传操作
	 				news.setPicPath(fullFile.getName());
	 			}
	 		
	 		}
	 		
	 	}
	 	
	 	//调用service方法进行数据库中保存操作 
		if(newsService.add(news)){ 
			//转发 
			//request.getRequestDispatcher(&quot;newsDetailList.jsp&quot;).forward(request, response); 
		%>
		<jsp:forward page="newsDetailList.jsp"/>
		<%}else{
			//重定向
			response.sendRedirect("newsDetailCreate.jsp");
	 	}
	 	
	} %>
	 
	 
	 

</body>
</html>